package com.jackrain.nea.web.common;

import com.alibaba.fastjson.JSON;
import com.jackrain.nea.core.redis.CusRedisTemplate;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.util.RedisOpsUtil;
import com.jackrain.nea.util.WebUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import static org.apache.shiro.SecurityUtils.getSubject;

@Slf4j
@Component
@Aspect
public class WebIntercept {

    public static String generateKey(HttpServletRequest request){
        String currentParam = JSON.toJSONString(request.getParameterMap());
        Subject subject = getSubject();
        String sessionId = (String)subject.getSession().getId();
        return "Request:"+sessionId+":"+ WebUtils.encryptBasedMD5(currentParam);
    }

    public static Boolean checkKey(HttpServletRequest request) throws NDSException, IOException {
        String key = generateKey(request);
        log.debug("WebAspect ->check "+key);
        CusRedisTemplate<String,String> redisTemplate = RedisOpsUtil.getObjRedisTemplate();
        Boolean isLock = redisTemplate.opsForValue().setIfAbsent(key,"OK");
        redisTemplate.expire(key,20, TimeUnit.MINUTES);
        request.setAttribute("isLock",isLock);
        return isLock;
    }

    @After(value = "execution(* com.jackrain.nea.web.controller.CommandController.*(..))&& args(request,..))")
    public static void removeKey(HttpServletRequest request) throws NDSException, IOException {
        String key = generateKey(request);
        log.debug("WebAspect ->remove "+key);
        if((Boolean) request.getAttribute("isLock")){
            CusRedisTemplate<String,String> redisTemplate = RedisOpsUtil.getObjRedisTemplate();
            redisTemplate.delete(key);
        }
    }

    @After(value = "execution(* com.jackrain.nea.web.controller.ImportController.importRecords(..))&& args(request,..))")
    public static void removeKeyWhenImport(HttpServletRequest request) throws NDSException, IOException {
        String key = generateKey(request);
        log.debug("WebAspect ->remove "+key);
        if((Boolean) request.getAttribute("isLock")){
            CusRedisTemplate<String,String> redisTemplate = RedisOpsUtil.getObjRedisTemplate();
            redisTemplate.delete(key);
        }
    }

    @After(value = "execution(* com.jackrain.nea.web.controller.ExportController.export(..))&& args(request,..))")
    public static void removeKeyWhenExport(HttpServletRequest request) throws NDSException, IOException {
        String key = generateKey(request);
        log.debug("WebAspect ->remove "+key);
        if((Boolean) request.getAttribute("isLock")){
            CusRedisTemplate<String,String> redisTemplate = RedisOpsUtil.getObjRedisTemplate();
            redisTemplate.delete(key);
        }
    }
}
